package com.xoms.pad.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import com.alibaba.fastjson.JSON;
import com.xoms.pad.controller.res.BaseResult;
import com.xoms.pad.controller.res.ListResult;
import com.xoms.pad.controller.res.ObjectResult;
import com.xoms.pad.controller.res.bean.RModifyOrderBean;
import com.xoms.pad.controller.res.bean.ROrderListBean;
import com.xoms.pad.controller.res.bean.RProductOrderBean;
import com.xoms.pad.controller.springmvc.req.GetOrderListParam;
import com.xoms.pad.controller.springmvc.req.ModifyOrderParam;
import com.xoms.pad.controller.springmvc.req.ProductOrderParam;
import com.xoms.pad.model.AccountBean;
import com.xoms.pad.service.OrderService;
import com.xoms.pad.service.exception.ServiceException;
import com.xoms.pad.service.res.PageResult;
import com.xoms.util.NumUtil;

/**
 * 订单相关接口
 * 
 * @author stallmandu
 *
 */
@Component
public class OrderController {
	private static Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	OrderService orderServiceImpl;

	/**
	 * 添加商品订单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	// http://localhost:8080/xoms-pad/order//v1/getProductOrder?token=01a12dd7e3384ee2ba7fb5f35f93ab64&pros=[{%20pid:%226d09358af9fd43128b1e92c4fc477335%22,price:%2015.00%20,num:%201}]&getMoney=0.00&payType=4&isHand=0&roomId=17c0f1b107934824a631e246740bf9c7&handRoomId=12&isAuth=0&authMoney=12.00&authAccId=01a12dd7e3384ee2ba7fb5f35f93ab64&sendStatus=0&deviceMac=12
	public ObjectResult<RProductOrderBean> addOrder(ProductOrderParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProductOrderBean> result = new ObjectResult<RProductOrderBean>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
//			List<OrderProJsonBean> pros = JSON.parseArray("[{pid:'6d09358af9fd43128b1e92c4fc477335',price:15.00,num:2}]", OrderProJsonBean.class);
			List<OrderProJsonBean> pros = JSON.parseArray(param.getPros(), OrderProJsonBean.class);
			double getMoney = Math.abs(NumUtil.toDouble(param.getGetMoney(), 0));
			int payType = NumUtil.toInt(param.getPayType(), 0);
			String roomId = param.getRoomId(); // 下单的影厅id
			int isHand = NumUtil.toInt(param.getIsHand(), 0);
			String handRoomId = param.getHandRoomId(); 
			int isAuth = NumUtil.toInt(param.getIsAuth(), 0); // 是否授权优惠 0未授权，1授权
			double authMoney = Math.abs(NumUtil.toDouble(param.getAuthMoney(), 0)); // 优惠金额
			String authAccountId = param.getAuthAccId(); // 优惠人id
			String deviceMac = param.getDeviceMac(); // 设备mac地址
			// 卖品下单
			RProductOrderBean data = orderServiceImpl.getAddOrder(account, pros, getMoney, payType, roomId,isHand, handRoomId,
					isAuth, authMoney, authAccountId, deviceMac);
			result.setFlag(true);
			result.setData(data);
			//微信支付的接口
			
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
			result.setFlag(false);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setFlag(false);
		}
		return result;
	}
	/**
	 * 获取商品订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<ROrderListBean> getOrderList(GetOrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<ROrderListBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String oid = param.getOid();
			int sendStatus = (param.getSendStatus() == null)? -1: param.getSendStatus();
			int status = (param.getStatus() == null)? -1: param.getStatus();
			int orderType = (param.getOrderType() == null)? -1: param.getOrderType();
			
			// 获取订单列表
			PageResult<ROrderListBean> resData = orderServiceImpl.orderList(account,oid,sendStatus,status,orderType,page, PageSize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
			result.setFlag(false);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setFlag(false);
		}
		return result;
	}
	
	
	
	
	/**
	 * 修改订单
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult modifyOrder(@Validated ModifyOrderParam param, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RModifyOrderBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
			String oid = param.getOid();
			String payType = param.getPayType();
			String isHand = param.getIsHand();
			String roomId = param.getRoomId();
			String handRoomId = param.getHandRoomId();
			String payStatus = param.getPayStatus();
			
			// 修改订单
			Boolean flag = orderServiceImpl.modifyOrder(account,oid,payType,isHand,roomId,handRoomId,payStatus);
			result.setFlag(flag);
			
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
			result.setFlag(false);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setFlag(false);
		}
		return result;
	}

}
